package com.example.day18_7_18_1.avtivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day18_7_18_1.R;
import com.example.day18_7_18_1.adapter.InfoRclAdapter;
import com.example.day18_7_18_1.bean.Bean;
import com.example.day18_7_18_1.presenter.Presenter;
import com.example.day18_7_18_1.view.IView;

import java.io.Serializable;

public class MainActivity extends AppCompatActivity implements IView<Bean> {

    private RecyclerView mRcl;
    private TextView mTvToolbar;
    private Toolbar mToolbar;
    private InfoRclAdapter infoRclAdapter;
    private Presenter presenter;
    private Bean mBean;
    private MyService myService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        presenter = new Presenter(this);
        initView();
        initData();
    }

    private void initData() {
        presenter.getData();
    }

    private void initView() {
        mRcl = (RecyclerView) findViewById(R.id.rcl);
        mTvToolbar = (TextView) findViewById(R.id.tv_toolbar);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mTvToolbar.setText("首页");
        mRcl.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRcl.setLayoutManager(new LinearLayoutManager(this));
        infoRclAdapter = new InfoRclAdapter(this);
        mRcl.setAdapter(infoRclAdapter);
        infoRclAdapter.setOnClick(new InfoRclAdapter.OnClick() {
            @Override
            public void onClick(int position) {
                sendBroadcast(position);
            }
        });
    }

    private void sendBroadcast(int position) {
        Intent intent = new Intent("com.x.y");
        intent.putExtra("data",mBean);
        intent.putExtra("title",mBean.getData().getDatas().get(position).getTitle());
        sendBroadcast(intent);
    }


    @Override
    protected void onResume() {
        super.onResume();
        myService = new MyService();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.x.y");
        registerReceiver(myService,intentFilter);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myService);
    }

    public class MyService extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String title = intent.getStringExtra("title");
            Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
            Bean data = (Bean) intent.getSerializableExtra("data");
            Intent pageIntent = new Intent(context, PageActivity.class);
            pageIntent.putExtra("data",data);
            context.startActivity(pageIntent);
        }
    }

    @Override
    public void setData(Bean bean) {
        mBean = bean;
        infoRclAdapter.setList(bean.getData().getDatas());
    }
}
